package cn.weiron.fun.schedule;


import cn.weiron.fun.sample.entity.SamplePoint;
import cn.weiron.fun.sample.entity.SamplePointTime;
import cn.weiron.fun.sample.entity.SampleTimeRecord;
import cn.weiron.fun.sample.service.SamplePointService;
import cn.weiron.fun.sample.service.SamplePointTimeService;
import cn.weiron.fun.sample.service.SampleTimeRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 点位排班记录定时器
 *
 *
 * @author weiRon
 * @date 2022/9/23 17:13
 */
@Component
@Configuration
@EnableScheduling
@Slf4j
public class PointTimeRecordSchedule {

    public PointTimeRecordSchedule(@Qualifier("samplePointServiceImpl") SamplePointService samplePointService,
                                   @Qualifier("samplePointTimeServiceImpl") SamplePointTimeService samplePointTimeService,
                                   @Qualifier("sampleTimeRecordServiceImpl") SampleTimeRecordService sampleTimeRecordService) {
        this.samplePointService = samplePointService;
        this.samplePointTimeService = samplePointTimeService;
        this.sampleTimeRecordService = sampleTimeRecordService;
    }


    private final  SamplePointService samplePointService;

    private final SamplePointTimeService samplePointTimeService;

    private final  SampleTimeRecordService sampleTimeRecordService;

    @Value("${feeType.one}")
    private int one;
    @Value("${feeType.two}")
    private int two;
    @Value("${feeType.three}")
    private int three;


    @Scheduled(cron = "0 10 0 * * ?")
    public void run() {
        if(true){
            log.info("定时任务启动");
            action();
        }
    }

    private void action(){
        List<SamplePoint> points = samplePointService.list();
        List<SampleTimeRecord> records = new ArrayList<>();
        String dateTime = LocalDateTime.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String week = LocalDateTime.now().minusDays(1).format(DateTimeFormatter.ofPattern("EEEE"));
        String year = LocalDateTime.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy"));
        String month = LocalDateTime.now().minusDays(1).format(DateTimeFormatter.ofPattern("M"));
        String day = LocalDateTime.now().minusDays(1).format(DateTimeFormatter.ofPattern("d"));
        points.forEach(point->{
            SampleTimeRecord sampleTimeRecord = new SampleTimeRecord();
            sampleTimeRecord.setCreateTime(LocalDateTime.now());
            sampleTimeRecord.setDateTime(dateTime);
            sampleTimeRecord.setPointUniqueId(point.getUniqueId());
            sampleTimeRecord.setYear(year);
            sampleTimeRecord.setMonth(month);
            sampleTimeRecord.setDay(day);

            QueryWrapper<SamplePointTime> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("sample_unique_id",point.getUniqueId());
            queryWrapper.eq("days",week);
            List<SamplePointTime> pointTimes = samplePointTimeService.list(queryWrapper);
            if(pointTimes!=null&&pointTimes.size()>0){
                SamplePointTime samplePointTime = pointTimes.get(0);
                sampleTimeRecord.setOpenTime(getOpenTime(samplePointTime));
                sampleTimeRecord.setHours(getTimeHours(samplePointTime));
                sampleTimeRecord.setPrice(new BigDecimal(1));
            }else{
                sampleTimeRecord.setOpenTime("0:00,24:00");
                sampleTimeRecord.setHours(0);
                sampleTimeRecord.setPrice(new BigDecimal(0));
            }
            //todo 扣款项
            records.add(sampleTimeRecord);
        });
        sampleTimeRecordService.saveBatch(records);
    }

    private static int getTimeHours(SamplePointTime samplePointTime){
        //todo 计时
        return 0;
    }
    private static String getOpenTime(SamplePointTime samplePointTime){
        StringBuffer sb = new StringBuffer();
        if(Strings.isNotBlank(samplePointTime.getAmBeginTime())){
            sb.append(samplePointTime.getAmBeginTime()+",");
        }
        if(Strings.isNotBlank(samplePointTime.getAmEndTime())){
            sb.append(samplePointTime.getAmEndTime()+",");
        }
        if(Strings.isNotBlank(samplePointTime.getPmBeginTime())){
            sb.append(samplePointTime.getPmBeginTime()+",");
        }
        if(Strings.isNotBlank(samplePointTime.getPmEndTime())){
            sb.append(samplePointTime.getPmEndTime()+",");
        }
        if(Strings.isNotBlank(samplePointTime.getEmBeginTime())){
            sb.append(samplePointTime.getEmBeginTime()+",");
        }
        if(Strings.isNotBlank(samplePointTime.getEmEndTime())){
            sb.append(samplePointTime.getEmEndTime()+",");
        }
        if(sb.length()>0){
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }
}
